﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.Text;
using NUnit.Framework;
using Obra.SmugMug.Api;
using Microsoft.Synchronization;
using Obra.Sync;

namespace Obra.Test
{
    /// <summary>
    /// This class is an integration test for the SmugMug WCF client, and the
    /// synchronization framework.
    /// </summary>
    /// <remarks>
    /// <para>
    /// I am using the HttpServer instead of SmugMugServer as the backing 
    /// server.  This means more work for me, but a little bit more coverage.
    /// I am not sure if it is worth it.
    /// </para>
    /// </remarks>
    [TestFixture]
    public class SmugMugSyncProviderTester
    {
        private Uri uri = new Uri(Messages.TestUrl);

        private MediaServiceProvider service;

        private SyncOrchestrator syncOrchestrator;

        [SetUp]
        public void SetUp()
        {
            this.service = new MediaServiceProvider(
                new WebHttpBinding(),
                new EndpointAddress(this.uri));

            this.syncOrchestrator = new SyncOrchestrator();
        }

        [TearDown]
        public void TearDown()
        {
        }

        private string GetLoginWithPasswordXml()
        {
            return File.ReadAllText(@"..\..\SmugMug\sample\SmugMugLoginWithPassword.xml");
        }

        private string GetAlbumsXml()
        {
            return File.ReadAllText(@"..\..\SmugMug\sample\SmugMugAlbumsGet.xml");
        }

        private string GetImagesXml()
        {
            return File.ReadAllText(@"..\..\SmugMug\sample\SmugMugImagesGet.xml");
        }

        [Test]
        public void Test()
        {
            using (HttpServerQueuedContent httpServer = new HttpServerQueuedContent(this.uri))
            {
                httpServer.ContentQueue.Enqueue(this.GetLoginWithPasswordXml());
                httpServer.ContentQueue.Enqueue(this.GetAlbumsXml());
                httpServer.ContentQueue.Enqueue(this.GetImagesXml());

                httpServer.Start();

                this.service.LogOn("username", "password");
                var albums = this.service.Albums;

                Assert.AreEqual(1, albums.Count);
                Assert.AreEqual("Fun in the Sun", albums[0].Name);
                Assert.IsEmpty(albums[0].Description);

                var images = albums[0].Media.ToList();

                Assert.AreEqual(6, images.Count);
                Assert.AreEqual("image-1.jpg", images[0].Name);
                Assert.AreEqual(3302937, images[0].SizeInBytes);
                Assert.AreEqual("image-2.jpg", images[1].Name);
                Assert.AreEqual(1140181, images[1].SizeInBytes);
            }
        }
    }
}
